package util;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import model.Catalog;
import model.Cover;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/6/19.
 */
public class CatalogToExcel {
    public static List<String> catalogList = new ArrayList<String>();
    public static Catalog catalog;



    public static void main(String[] args) {
        String rootPath = "C:\\Users\\Administrator\\Desktop\\A3\\目录";
        String excel = "C:\\Users\\Administrator\\Desktop\\A3\\目录\\catalog.xls";

        try{
            WritableWorkbook wwb = null;
            //首先要使用Workbook类的工厂方法创建一个可写入的工作薄(Workbook)对象
            wwb = Workbook.createWorkbook(new File(excel));

            File root = new File(rootPath);
            if(root.isDirectory()){
                File[] catalogs = root.listFiles();
                for (int i = 0; i < catalogs.length; i++) {
                    if(wwb!=null){
                        File catalog = catalogs[i];
                        if (catalog.isDirectory()){
                            //创建一个可写入的工作表
                            WritableSheet ws = wwb.createSheet(catalog.getName(), i);
                            System.out.println("已写入："+catalog.getName());
                            if(catalog.isDirectory()){
                                File[] courses = catalog.listFiles();
                                int rowNum = 0;
                                for (int j = 0; j < courses.length; j++) {
                                    File course = courses[j];
                                    if(course.isDirectory()){
                                        File[] courseDetial = course.listFiles();
                                        int size = courseDetial.length;
                                        if(size == 0){
                                            Label fileName = new Label(0,rowNum,course.getName());
                                            ws.addCell(fileName);
                                            Label filePath = new Label(1,rowNum++,course.getPath());
                                            ws.addCell(filePath);
                                        }
                                        for (int k = 0; k < size; k++) {
//                                            Label label = new Label(0,j,course.getName());
                                            Label label = new Label(0,rowNum,courseDetial[k].getName());
                                            ws.addCell(label);
                                            Label filePath = new Label(1,rowNum++,courseDetial[k].getPath());
                                            ws.addCell(filePath);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                wwb.write();
                wwb.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
    /**
     * 将级联文件夹生成到Excel中
     * @param excel excel
     * @param scource 目标文件夹
     * @return
     */
    public boolean toExcel(File excel,File scource){
            return false;
    }

    /**
     * 遍历文件夹，生成集合对象
     * @param source 根文件夹
     * @return 生成集合对象
     */
    public static Catalog traversal(File source){
        if(source == null){
            System.out.println("没有源文件");
        }else{
            if(source.isDirectory()){
                File[] file = source.listFiles();
                for (File catalogFile : file) {
                    String name = catalogFile.getName();
                    catalog.setCatalogName(name);

                }
            }
        }
        return  catalog;
    }

    public static void writeExcel(File Destination, List<String> catalogList){
        WritableWorkbook wwb = null;
        try {
            //首先要使用Workbook类的工厂方法创建一个可写入的工作薄(Workbook)对象
            wwb = Workbook.createWorkbook(Destination);
            if(wwb!=null){
                for(int i = 0; i < catalogList.size();i++){
                    //创建一个可写入的工作表
                    WritableSheet ws = wwb.createSheet(catalogList.get(i),i);
                }
                wwb.write();
                wwb.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (RowsExceededException e) {
            e.printStackTrace();
        } catch (WriteException e) {
            e.printStackTrace();
        }finally {
            System.out.println("已完成写入");
        }
    }
}
